package com.unicom.vw.modules.sys.oauth2;

import java.util.Map;

import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.unicom.vw.common.utils.R;
import com.unicom.vw.modules.sys.entity.SysUserEntity;
import com.unicom.vw.modules.sys.service.SysUserService;
import com.unicom.vw.modules.sys.service.SysUserTokenService;

@RestController
@RequestMapping("/app")
public class AppOauthToken {
	
	@Autowired
	private SysUserService sysUserService;
	@Autowired
	private SysUserTokenService sysUserTokenService;
	
	@RequestMapping("/oauth/token")
	public Map<String, Object> oauthToken(String username, String password){
		
		//用户信息
		SysUserEntity user = sysUserService.queryByUserName(username);

		//账号不存在、密码错误
		if(user == null || !user.getPassword().equals(new Sha256Hash(password, user.getSalt()).toHex())) {
			return R.error("账号或密码不正确");
		}

		//账号锁定
		if(user.getStatus() == 0){
			return R.error("账号已被锁定,请联系管理员");
		}

		//生成token，并保存到数据库
		R r = sysUserTokenService.createToken(user.getUserId());
		return r;
	}
	
}
